<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2019/7/29
 * Time: 16:45
 */

namespace tools;


class MyHelp
{
    protected $error;

    public function getError(){
        return $this->error;
    }

    public function chackSign ($data,&$param,$type){
        ksort($param);
        $str = http_build_query($param);

        if($type!=1){
            $redis = new Redis();
            $token = $redis->getAndExpire($data['apid'].'.token');
            if(!$token){
                $this->error = ['code'=>1004,'msg'=>'登陆过期'];
                return false;
            }
            $str = $redis->getAndExpire($data['apid'].'.token').$str;
        }

        if($data['sign']!=strtoupper(md5($str))){
            $this->error = ['code'=>1001,'msg'=>'签名错误'];
            return false;
        }

        $ctime = $_SERVER['REQUEST_TIME']-(int)$data['timestamp'];
        if( $ctime >30 || $ctime<0){
            $this->error = ['code'=>1005,'msg'=>'请求超时'];
            return false;
        }
        unset($param['timestamp']);
        return true;

    }

    public function create_guid($namespace = '') {
        static $guid = '';
        $uid = uniqid("", true);
        $data = $namespace;
        $data .= $_SERVER['REQUEST_TIME'];
        $data .= $_SERVER['HTTP_USER_AGENT'];
        $data .= $_SERVER['REMOTE_ADDR'];
        $data .= $_SERVER['REMOTE_PORT'];
        $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
        $guid =
            substr($hash, 0, 8) .
            '-' .
            substr($hash, 8, 4) .
            '-' .
            substr($hash, 12, 4) .
            '-' .
            substr($hash, 16, 4) .
            '-' .
            substr($hash, 20, 12);
        return $guid;
    }
}